//
//  ZSLTabBar.swift
//  micro_blog
//
//  Created by Mac on 16/9/19.
//  Copyright © 2016年 Mac. All rights reserved.
//

import UIKit

protocol ZSLTabBarDelegate: NSObjectProtocol
{
    func didSelectedComposeButton()
}

class ZSLTabBar: UITabBar {
    
    var composeBtnClosure: (()->())?
    
    weak var newDelegate: ZSLTabBarDelegate?

    private lazy var composeBtn: UIButton = {
       
        let button = UIButton()
        
        button.setImage(UIImage(named:"tabber_compose_icon_add"), for: .normal)
        
     button.setImage(UIImage(named:"tabber_compose_icon_add_highlighted"), for: .highlighted)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        
        return button
        
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }
    
    private func setupUI(){
        
        backgroundImage = UIImage(named: "tabbar_background")
        
        addSubview(composeBtn)
        
    }
    
    @objc private func composeButtonAction() {
        
        composeBtnClosure?()
        
        newDelegate?.didSelectedComposeButton()
    }
    
    //设置控件的位置
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        composeBtn.centerX = width * 0.5
        
        composeBtn.centerY = height * 0.5
        
        let itemWidth = width / 5
        
        var index = 0
        
        for value in subviews
        {
            
            if value.isKind(of: NSClassFromString("UITabBarButton")!)
            {
                value.width = itemWidth
                
                index += 1
                
                if index == 2
                {
                    index += 1
                    
                }
            }
        }
        
        
        
    }

}
